package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.SysUser;
import com.hmall.service.HistoryService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.HistoryVo;
import com.hmall.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

/**
 * @Author zhoutao
 * @Date 2018/11/8 13:04
 * @Param
 * @return
 * @Description //
 */
@RestController
@RequestMapping("/history/")
@CrossOrigin(origins = "http://localhost:8088", maxAge = 3600)
@Api(value = "历史记录API", description = "历史记录API说明")
public class HistoryController {

    private final JedisPool jedisPool;

    private final HistoryService iHistoryService;

    TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public HistoryController(JedisPool jedisPool, HistoryService iHistoryService) {
        this.jedisPool = jedisPool;
        this.iHistoryService = iHistoryService;
    }

    /**
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Date 2018/11/8 16:28
     * @Param [token, pageNum, pageSize]
     * @Description // 足迹
     */
    @LoginRequired
    @PostMapping("viewHistory")
    @ApiOperation(value = "浏览历史", notes = "删除的主键是historyId")
    public ServerResponse<PageVo<HistoryVo>> viewHistory(String token, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {


        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iHistoryService.viewHistory(user.getUserId(), pageNum, pageSize);
    }

    @LoginRequired
    @DeleteMapping("deleteHistory")
    @ApiOperation(value = "删除单条历史")
    public ServerResponse deleteHistory(String token, Integer productId) {

        return iHistoryService.deleteHistory(productId);
    }

    @LoginRequired
    @DeleteMapping("deleteAllHistory")
    @ApiOperation(value = "删除所有历史")
    public ServerResponse deleteAllHistory(String token) throws Exception {


        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();

        return iHistoryService.deleteAllHistory(user);
    }


}
